﻿using System.Data.SqlClient;
using System.IO;
using System.Data;
using System.Runtime.Serialization;

namespace DM4
{
    [DataContract(Name = "XMLInfo")]
    public class XMLInfo
    {
        [DataMember(Name = "xsd", Order = 0, IsRequired = true)]
        public string XSD;

        [DataMember(Name = "xml", Order = 1, IsRequired = true)]
        public string XML;

        [DataMember(Name = "sessId", Order = 3, IsRequired = true)]

        public OperationResult OperResult = null;

        public XMLInfo() { }

        public XMLInfo(OperationResult operResult)
        {
            OperResult = operResult;
        }

        public static DataTable FillTable(XMLInfo xi, DataTable table = null)
        {
            DataTable tmptable = table ?? new DataTable();

            if (tmptable.Columns.Count == 0)
            {
                tmptable.ReadXmlSchema(new StringReader(xi.XSD));
            }
            else
            {
                tmptable.Rows.Clear();
            }

            tmptable.ReadXml(new StringReader(xi.XML));
            return tmptable;
        }

        public XMLInfo Load(SqlDataReader reader, string tablename)
        {
            DataTable dt = new DataTable(tablename);
            dt.Load(reader);
            return Load(dt);
        }

        //public static XMLInfo Create(SqlDataReader reader, string tablename)
        //{
        //    DataTable dt = new DataTable(tablename);
        //    dt.Load(reader);
        //    return Create(dt);
        //}


        //public static XMLInfo Create(DataTable sourcetable, DataRow[] datarows = null)
        //{
        //    XMLInfo xi = new XMLInfo();
        //    if (datarows != null)
        //    {

        //        using (DataTable table = sourcetable.Clone())
        //        {
        //            foreach (DataRow dr in datarows)
        //            {
        //                table.ImportRow(dr);
        //            }

        //            using (StringWriter sw = new StringWriter())
        //            {
        //                table.WriteXmlSchema(sw);
        //                xi.XSD = sw.ToString();
        //            }

        //            using (StringWriter sw = new StringWriter())
        //            {
        //                table.WriteXml(sw);
        //                xi.XML = sw.ToString();
        //            }
        //        }
        //    }
        //    else
        //    {
        //        using (DataTable table = sourcetable.Copy())
        //        {
        //            using (StringWriter sw = new StringWriter())
        //            {
        //                table.WriteXmlSchema(sw);
        //                xi.XSD = sw.ToString();
        //            }

        //            using (StringWriter sw = new StringWriter())
        //            {
        //                table.WriteXml(sw);
        //                xi.XML = sw.ToString();
        //            }
        //        }
        //    }

        //    return xi;
        //}

        public XMLInfo Load(DataTable sourcetable, DataRow[] datarows = null)
        {
            if (datarows != null)
            {

                using (DataTable table = sourcetable.Clone())
                {
                    foreach (DataRow dr in datarows)
                    {
                        table.ImportRow(dr);
                    }

                    using (StringWriter sw = new StringWriter())
                    {
                        table.WriteXmlSchema(sw);
                        XSD = sw.ToString();
                    }

                    using (StringWriter sw = new StringWriter())
                    {
                        table.WriteXml(sw);
                        XML = sw.ToString();
                    }
                }
            }
            else
            {
                using (DataTable table = sourcetable.Copy())
                {
                    using (StringWriter sw = new StringWriter())
                    {
                        table.WriteXmlSchema(sw);
                        XSD = sw.ToString();
                    }

                    using (StringWriter sw = new StringWriter())
                    {
                        table.WriteXml(sw);
                        XML = sw.ToString();
                    }
                }
            }

            return this;
        }
    }
}
